/**
 * 
 */
package com.wordpress.music2dot0.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author raf
 * 
 */
public class LoginServer {
	public static Logger log = Logger.getLogger("MusicServer");
	private static final int PORT = 5000;
	private static GroupServer gs; // TODO multiple group servers

	public static void acceptConnections() {
		try {
			final ServerSocket serverSocket = new ServerSocket(PORT);
			Socket clientSocket;
			log.info("LoginServer running, awaiting connections");
			while ((clientSocket = serverSocket.accept()) != null) {
				log.info("New connection: " + clientSocket.getInetAddress());
				final Connection runnable = new Connection(clientSocket);
				final Thread thread = new Thread(runnable);
				thread.start();
			}
		} catch (final IOException e) {
			e.printStackTrace();
		}
	}

	public static void joinGroup(Connection conn) {
		log.fine("Connection joining group");
		gs.addConnection(conn);
		conn.joinGroup(gs);
	}

	public static void main(String[] args) {
		log.setLevel(Level.FINEST);
		gs = new GroupServer();
		gs.playNext();
		acceptConnections();
	}
}
